using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Gestion_Comercial._2_Logica_de_Negocio;

namespace Gestion_Comercial
{
    public partial class ContactoDetalle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["ID"] == "-1")
                CambiarModo(FormViewMode.Insert);
            
        }

        protected void lnkInsertar_Click(object sender, EventArgs e)
        {
            CambiarModo(FormViewMode.Insert);
        }

        protected void lnkEdiar_Click(object sender, EventArgs e)
        {
            CambiarModo(FormViewMode.Edit);
        }
        private void CambiarModo(FormViewMode pModo)
        {
            fvContacto.ChangeMode(pModo);
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            CambiarModo(FormViewMode.ReadOnly);
        }


        protected void fvContacto_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            
        }

        protected void fvContacto_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
CambiarModo(FormViewMode.ReadOnly);
        }

        protected void fvContacto_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            Response.Redirect("Contactos.aspx");
        }

        protected void dsContacto_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            int id = ((ONContacto)e.ReturnValue).ID;
            Response.Redirect("ContactoDetalle.aspx?ID=" + id.ToString());
        //myObjectDataSource.SelectParameters["ID"] = id;
    
        //myFormView.DataBind();
        }

        protected void cmbClienteAsociado_DataBound(object sender, EventArgs e)
        {
            if ((Request.QueryString["ID"] == "-1")
                && (Request.QueryString["IDC"] != null)
                && (Request.QueryString["IDC"] != ""))
                try
                {//selecciono el cliente en el combo
                    ((DropDownList)sender).SelectedValue = Request.QueryString["IDC"];
                }
                catch { ((DropDownList)sender).SelectedIndex = -1; }
                
        }

        protected void cmbClienteAsociado_DataBinding(object sender, EventArgs e)
        {
            
        }

        protected void cmbClienteAsociadoEdit_DataBound(object sender, EventArgs e)
        {
try
            {
                DropDownList _ddl = (DropDownList)sender;
                bool _encontrado = false;
                int _idCliente = int.Parse(_ddl.SelectedValue);

                string d = e.ToString();

                if (_idCliente > -1)
                {
                    for (int i = 0; i < _ddl.Items.Count; i++)
                        if (_ddl.Items[i].Value == _idCliente.ToString())
                        {
                            _ddl.SelectedIndex = i;
                            _encontrado = true;
                        }

                    if (_encontrado == false)
                    {
                        ONCliente _cliente = ONCliente.CrearCliente(_idCliente);
                        ListItem _li = new ListItem(_cliente.RazonSocial, _cliente.ID.ToString());
                        _ddl.Items.Insert(0, _li);
                        _ddl.SelectedIndex = 0; // elijo el primero
                    }
                }
            }
            catch
            {
               // MostrarError("El turno no existe", true);
            }
        }

        protected void dsClientes_SelectingMaster(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            ((Site1)Master).AsignarParametrosParaBuscarClientesDeComercialEnMaster(sender,e);

            //e.InputParameters["pUsuarioActual"] = HttpContext.Current.User.Identity.Name;
            //string _rol = ""; //verifico si pertenece al rol de gerentes
            //if (HttpContext.Current.User.IsInRole(ConfigurationSettings.AppSettings["GerenteRol"]) == true)
            //    _rol = ConfigurationSettings.AppSettings["GerenteRol"];
            //e.InputParameters["pRolActual"] = _rol;
        }

        protected void lnkClienteAsociado_Click(object sender, EventArgs e)
        {
            string _id = ((LinkButton)sender).CommandArgument.ToString();
            Response.Redirect("ClienteDetalle.aspx?ID=" + _id);
        }

        protected void cmbClienteAsociadoEdit_DataBinding(object sender, EventArgs e)
        {
            bool _encontrado = false;
            int _id;
            DropDownList _ddl = (DropDownList)sender;

            if (Request["ID"] != null)
            {
                _id = int.Parse(Request["ID"]);

                //creo el objeto Contacto para obtener el cliente
                ONContacto _objeto = ONContacto.CrearContacto(_id);

                if (_objeto.IdCliente > -1)
                {
                    int _idCliente = _objeto.IdCliente;
                    for (int i = 0; i < _ddl.Items.Count; i++)
                        if (_ddl.Items[i].Value == _idCliente.ToString())
                        {
                            _ddl.SelectedIndex = i;
                            _encontrado = true;
                        }

                    if (_encontrado == false)
                    {// si no se encontro creo el elemento y lo agrego al combo
                        ONCliente _cliente = ONCliente.CrearCliente(_idCliente);
                        ListItem _li = new ListItem(_cliente.RazonSocial, _cliente.ID.ToString());
                        _ddl.Items.Insert(1, _li);
                        _ddl.SelectedIndex = 1; // elijo el primero
                    }
                }
            }
        }

    }
}
